'use strict';

// 写增删改查的业务逻辑
// 写在Service里 然后controller调用Service
const Service = require('egg').Service;

// async find(uid) {
// // 假如 我们拿到用户 id 从数据库获取用户详细信息
// // const user = await this.app.mysql.get('users', { id: 11 });
//   const user = await this.app.mysql.select('user');
//   console.log('find:', user);
//   return { user };
// }

class UserService extends Service {
  async addUser (user) {
    console.log('--------------addUser-----------------------');
    console.log(user);
    user.register_time = new Date();
    // 保存用户信息到数据库
    const users = await this.app.model.User.create(user);
    // 返回保存用户的信息
    return users;
  }
  // 通过账号密码查询数据库
  async findUser (user) {
    console.log('--------------findUser-----------------------');
    console.log(user);

    // 根据账号和密码去查询数据库，findOne是查一条数据，where是传查询条件
    const users = await this.app.model.User.findOne({
      where: {
        account: user.account,
        password: user.password,
      },
    });
    console.log(users);
    // 返回保存用户的信息
    return users;
  }
  // 查询权限用户
  async getUserListHavePermission (params) {
    console.log('--------------getUserListHavePermission-----------------------');
    const Op = this.app.Sequelize.Op; // 获取Sequelize的Op
    // 查询所有字段，增加查询条件为permission_id不为空
    const page = params.page;
    const page_size = params.limit;
    const offset = (page - 1) * page_size;
    const limit = parseInt(page_size);

    const user_count = await this.app.model.User.findAll({
      where: {
        status: {
          [Op.in]: [2],
        },
      }
    });
    const user = await this.app.model.User.findAll({
      where: {
        status: {
          [Op.in]: [2],
        },
      }, offset,
      limit
    });
    return { total: user_count.length, user };
  }

  // 查询普通用户
  async getUserList (params) {
    console.log('-----------------------getUserList----------------------------');
    const Op = this.app.Sequelize.Op; // 获取Sequelize的Op
    // console.log(Op);
    const page = params.page;
    const page_size = params.limit;
    const offset = (page - 1) * page_size;
    const limit = parseInt(page_size);
    // 查询所有字段，增加查询条件为permission_id不为空
    const user_count = await this.app.model.User.findAll({
      where: {
        status: {
          [Op.in]: [0, 3],
        },
      }, attributes: ['id', 'account', 'password', 'img', 'nackname', 'sex', 'age', 'email', 'address', 'status', 'register_time']
    }
    );

    const user = await this.app.model.User.findAll({
      where: {
        status: {
          [Op.in]: [0, 3],
        },
      }, attributes: ['id', 'account', 'password', 'img', 'nackname', 'sex', 'age', 'email', 'address', 'status', 'register_time'],
      offset,
      limit
    }
    );
    // console.log(user);
    return { total: user_count.length, user };
  }

  // 查询申请权限用户-审批
  async getUserListApply (params) {
    const Op = this.app.Sequelize.Op;
    console.log('----------------------getUserListApply-----------------------');
    //  const Op = this.app.Sequelize.Op; // 获取Sequelize的Op
    // console.log(Op);
    // 查询所有字段，增加查询条件为permission_id不为空
    const page = params.page;
    const page_size = params.limit;
    const offset = (page - 1) * page_size;
    const limit = parseInt(page_size);


    const user_count = await this.app.model.User.findAll({
      where: {
        status: {
          [Op.in]: [1],
        },
      }
    });

    const user = await this.app.model.User.findAll({
      where: {
        status: {
          [Op.in]: [1],
        },
      },
      offset,
      limit
    });
    return { total: user_count.length, user };
  }

  async getUserListByyihu (params) {
    const Op = this.app.Sequelize.Op;
    console.log('----------------------getUserListByyihu-----------------------');
    //  const Op = this.app.Sequelize.Op; // 获取Sequelize的Op
    // console.log(Op);
    // 查询所有字段，增加查询条件为permission_id不为空
    const page = params.page;
    const page_size = params.limit;
    const offset = (page - 1) * page_size;
    const limit = parseInt(page_size);


    const user_count = await this.app.model.User.findAll({
      where: {
        role: {
          [Op.in]: [2, 3],
        },
      }
    });

    const user = await this.app.model.User.findAll({
      where: {
        role: {
          [Op.in]: [2, 3],
        },
      },
      offset,
      limit
    });
    return { total: user_count.length, user };
  }

  async geYihuDetail (id) {
    console.log('----------------------geYihuDetail-----------------------');
    const { QueryTypes } = require('sequelize');
    const yihu = await this.app.model.query(
      'SELECT u.*,r.experience,r.price,r.door_time,r.specialty,r.top_img FROM user u,`release` r WHERE u.id = r.user_id AND r.id=:id',
      {
        replacements: { id },
        type: QueryTypes.SELECT,
      }
    );
    return yihu;
  }

  async getHugong () {

    console.log('----------------------getHugong-----------------------');
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT r.*,u.college,u.realname FROM user u,`release` r WHERE u.id = r.user_id AND r.`status` =1',
      {

        type: QueryTypes.SELECT,
      }
    );
    return list;

  }

  // 模糊查询
  async likeQuerying (params) { // /
    console.log('----------------------likeQuerying-----------------------');
    console.log(params);
    let account = '%%';
    let nackname = '%%';
    let role = '%%';
    // const status = '';
    // hasOwnProperty:判断参数是否含有这个key

    if (params.hasOwnProperty('account')) {
      account = '%' + params.account + '%'; // 定义一个变量去接收
    }
    if (params.hasOwnProperty('nackname')) {
      nackname = '%' + params.nackname + '%'; // 定义一个变量去接收
    }
    if (params.hasOwnProperty('role')) {
      role = '%' + params.role + '%'; // 定义一个变量去接收
    }

    const Op = this.app.Sequelize.Op; // 获取Sequelize的Op
    // console.log(Op);
    // 查询所有字段，增加查询条件为permission_id不为空
    const user = await this.app.model.User.findAll({
      where: {

        status: {
          [Op.in]: params.status,
        },
        account: {
          [Op.like]: account,
        },
        nackname: {
          [Op.like]: nackname,
        },
        role: {
          [Op.like]: role,
        },
      }
    });
    return user;
  }

  // 审批
  async approvingUser (id, status, transaction) {
    console.log('-----------approvingUser------------');
    console.log(status);
    console.log(id);
    return await this.app.model.User.update({
      status,
    }, {
      where: {
        id,
      }, transaction,
    });
  }

  async findUserByAccount (account, transaction) {
    console.log('-----------findUserByAccount------------');
    console.log(account);
    return await this.app.model.User.findOne({
      where: {
        account,
      }, transaction,
    });
  }


}

module.exports = UserService;
